<template>
  <div class="exception-container">
    <h1 class="exception-title">异常记录管理</h1>

    <!-- 查询表单 -->
    <el-card class="search-card">
      <el-form :model="queryParams" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="订单ID">
              <el-input
                v-model="queryParams.orderId"
                placeholder="请输入订单ID"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="处理状态">
              <el-select
                v-model="queryParams.status"
                placeholder="请选择状态"
                clearable
              >
                <el-option label="已处理" value="resolved" />
                <el-option label="处理中" value="pending" />
                <el-option label="未处理" value="cancelled" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="search-buttons">
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
      >
        <el-table-column
          prop="recordId"
          label="记录ID"
          width="100"
          align="center"
        />
        <el-table-column
          prop="orderId"
          label="订单ID"
          width="100"
          align="center"
        />
        <el-table-column
          prop="exceptionType"
          label="异常类型"
          width="120"
          align="center"
        >
          <template #default="{ row }">
            <el-tag :type="getExceptionTypeTag(row.exceptionType)">
              {{ row.exceptionType }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="异常描述" min-width="180" />
        <el-table-column
          prop="handlerId"
          label="处理人ID"
          width="100"
          align="center"
        />
        <el-table-column
          prop="status"
          label="处理状态"
          width="120"
          align="center"
        >
          <template #default="{ row }">
            <el-tag :type="getStatusTag(row.status)">
              {{ row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="solution" label="解决方案" min-width="180" />
        <el-table-column label="操作" width="150" align="center" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-left">
        <el-pagination
          v-model:current-page="queryParams.page"
          v-model:page-size="queryParams.size"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
      <el-form :model="formData" label-width="100px">
        <el-form-item label="异常类型">
          <el-select
            v-model="formData.exceptionType"
            placeholder="请选择异常类型"
          >
            <el-option label="缺货" value="缺货" />
            <el-option label="延迟" value="延迟" />
            <el-option label="质量问题" value="质量问题" />
            <el-option label="其他" value="其他" />
          </el-select>
        </el-form-item>
        <el-form-item label="异常描述">
          <el-input v-model="formData.description" type="textarea" :rows="3" />
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="formData.status" placeholder="请选择处理状态">
            <el-option label="未处理" value="未处理" />
            <el-option label="处理中" value="处理中" />
            <el-option label="已处理" value="已处理" />
          </el-select>
        </el-form-item>
        <el-form-item label="解决方案">
          <el-input v-model="formData.solution" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import axios from "axios";

// 加载状态
const loading = ref(false);

// 查询参数
const queryParams = reactive({
  page: 1,
  size: 10,
  orderId: null,
  status: null,
});

// 表格数据
const tableData = ref([]);
const total = ref(0);

// 对话框相关
const dialogVisible = ref(false);
const dialogTitle = ref("");
const formData = reactive({
  recordId: null,
  orderId: null,
  exceptionType: "",
  description: "",
  handlerId: null,
  status: "",
  solution: "",
});

// 获取异常记录列表
const fetchData = async () => {
  try {
    loading.value = true;
    const response = await axios.post(
      "http://localhost:8081/exception-records/list",
      queryParams,
      {
        headers: {
          "Content-Type": "application/json",
        },
      }
    );
    if (response.data.code === 200) {
      // 修改这里，匹配后端返回的数据结构
      tableData.value = response.data.data.list;
      total.value = response.data.data.count;
    } else {
      ElMessage.error(response.data.message || "获取数据失败");
    }
  } catch (error) {
    ElMessage.error("请求失败: " + error.message);
  } finally {
    loading.value = false;
  }
};

// 查询
const handleQuery = () => {
  queryParams.page = 1;
  fetchData();
};

// 重置查询
const resetQuery = () => {
  queryParams.orderId = null;
  queryParams.status = null;
  handleQuery();
};

// 分页大小改变
const handleSizeChange = (val) => {
  queryParams.size = val;
  fetchData();
};

// 当前页改变
const handleCurrentChange = (val) => {
  queryParams.page = val;
  fetchData();
};

// 编辑记录
const handleEdit = (row) => {
  dialogTitle.value = "编辑异常记录";
  Object.assign(formData, row);
  dialogVisible.value = true;
};

// 提交表单
const submitForm = async () => {
  try {
    const response = await axios({
      method: 'post',
      url: 'http://localhost:8081/exception-records/update',
      headers: {
        'Content-Type': 'application/json' // 必须设置这个请求头
      },
      data: JSON.stringify(formData) // 必须将对象转为JSON字符串
    });

    if (response.data.code === 200) {
      ElMessage.success("操作成功");
      dialogVisible.value = false;
      fetchData();
    } else {
      ElMessage.error(response.data.message || "操作失败");
    }
  } catch (error) {
    ElMessage.error("操作失败: " + error.message);
  }
};

// 获取异常类型标签样式
const getExceptionTypeTag = (type) => {
  switch (type) {
    case "缺货":
      return "danger";
    case "延迟":
      return "warning";
    case "质量问题":
      return "danger";
    default:
      return "info";
  }
};

// 获取状态标签样式
const getStatusTag = (status) => {
  switch (status) {
    case "已处理":
      return "success";
    case "处理中":
      return "warning";
    case "未处理":
      return "danger";
    default:
      return "info";
  }
};

// 初始化加载数据
onMounted(() => {
  fetchData();
});
</script>

<style scoped>
.exception-container {
  padding: 20px;
  background-color: #fff;
}

.exception-title {
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.search-card {
  margin-bottom: 20px;
}

.search-buttons {
  display: flex;
  justify-content: flex-end;
}

.table-card {
  margin-bottom: 20px;
}

/* 修改后的样式 */
.pagination-left {
  margin-top: 20px;
  display: flex;
  justify-content: flex-start;
  margin-left: 0;
}

.el-table {
  margin-top: 10px;
}

.el-tag {
  margin: 2px;
}
</style>